<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:39
 */

namespace app\admin\controller;

use app\admin\model\GiftUrl;
use app\admin\model\GoldRoomEggPrize as GoldRoomEggPrizeModel;
use app\admin\model\GoldRoom as GoldRoomModel;
use app\admin\model\User as UserModel;
use app\admin\model\Room as RoomModel;
use app\admin\model\GoldEgg as GoldEggModel;
use app\admin\model\Gift as GiftModel;
use app\admin\model\User;
use think\cache\driver\Redis;
/**
 * 砸金蛋活动
 * Class GoldEgg
 * @package app\admin\controller
 */
class GoldRoom extends Index
{
    /**
     * @package 渲染砸蛋房
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }
    /**
     * @package 渲染砸蛋房数据
     * @return \think\response\View
     */
    public function indexData()
    {
        $goldRoomModel = new GoldRoomModel();
        $data = $goldRoomModel->getDataList();
        return json($data);
    }

    /**
     * @package 添加砸蛋房
     * @return \think\response\View
     */
    public function add()
    {
        if (request()->isPost()) {

            $userModel = new UserModel();
            $roomModel = new RoomModel();
            $goldRoomModel = new GoldRoomModel();
            $userData = $userModel->where(['u_number' => input('u_id')])->find();
            $roomData = $roomModel->where(['user_id' => $userData['id']])->find();
            $newData['room_id'] = $roomData['id'];
            $newData['u_id'] = $userData['user_id'];
            //日志数据
            try {
                $goldRoomModel->save($newData);
            } catch (\Exception $e) {
                $this->error(lang('add') . lang('false') . $e->getMessage());
            }
            $this->success(lang('add') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * @package 取消砸蛋房
     * @return \think\response\Redirect
     */
    public function del()
    {
        if (request()->isPost()) {
            $goldRoomModel = new GoldRoomModel();

            $goldRoomDetail = $goldRoomModel->where(['id' => input('id')])->find();
            try {
                $goldRoomModel->where(['id' => input('id')])->delete();
            } catch (\Exception $e) {
                $this->error(lang('delete') . lang('false') . $e->getMessage());
            }
            $this->success(lang('delete') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * @package 砸蛋房金蛋奖品
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function prizeIndex()
    {
        $goldEggModel = new GoldEggModel();
        $giftModel = new GiftModel();
        $gold_egg_list = $goldEggModel->getDataList();
        return view()->assign(['gold_egg_list' => $gold_egg_list]);
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     *
     */
    public function prizeIndexData()
    {
        $goldRoomEggPrizeModel = new GoldRoomEggPrizeModel();
        $data = $goldRoomEggPrizeModel->getDataList();
        return json($data);
    }

    /**
     * 修改砸蛋房奖品
     * @return \think\response\Redirect
     */
    public function prizeEdit()
    {
        $goldRoomEggPrizeModel = new GoldRoomEggPrizeModel();
        $res = $goldRoomEggPrizeModel->prizeEdit();
        return $res;

    }

    /**
     * 砸蛋房添加宝箱奖品
     */
    public function prizeSave()
    {
        $goldRoomEggPrizeModel = new GoldRoomEggPrizeModel();
        $res = $goldRoomEggPrizeModel->prizeSave();

        if ($res) $this->success(lang('add') . lang('success'));
        else  $this->error(lang('add') . lang('false'));

        return redirect('/Miss');
    }
    /**
     * 上架下架
     */
    public function changePrizeStatus()
    {
        $goldRoomEggPrizeModel = new GoldRoomEggPrizeModel();
        $res = $goldRoomEggPrizeModel->changePrizeStatus();
        return $res;
    }

    //动态获取奖品或是道具
    public function boxGift()
    {
        $goldRoomEggPrizeModel = new GoldRoomEggPrizeModel();
        $res = $goldRoomEggPrizeModel->boxGift();
        return $res;

    }



    public function goldRoomSendUserGiftData()
    {
        $redis = new Redis();
        $point = $redis->getKeys('gold_room_looty:*');
        $arr = [];
        foreach($point as $k=>$v){
            $arr[] = intval(substr($v,16));
        }

        $data = User::where('id','IN',$arr)->field('id,u_id,nickname')->paginate();

        foreach($data as $k=>$v){
            $data[$k]['current_num'] = $redis->get('gold_room_looty:'.$v['id']);  //目前抽奖的次数
            $data[$k]['total_num'] = $redis->get('gold_room_total_looty:'.$v['id']);  //指定抽奖的次数
           // $data[$k]['id'] = $redis->get('gold_room_gift_looty:'.$v['id']);  //指定抽中的奖品,库存id

            $id = $redis->get('gift_looty:'.$v['id']);  //指定抽中的奖品,库存id
            $gift_id = GoldRoomEggPrizeModel::where('id','=',$id)->value('gift_id');
            $gift_name = GiftUrl::where('id','=',$gift_id)->value('gift_name');
            $data[$k]['gift_name'] = $gift_name;

            if(empty($data[$k]['total_num']))  $data[$k]['total_num'] = 0;
            if(empty($data[$k]['id']))  $data[$k]['id'] = 0;
        }

        return json($data);
    }

    public function goldRoomSendUserGiftAdd()
    {
        $redis = new Redis();
        $u_id = input('post.u_id');
        $total_num = input('post.total_num');
        $id = input('post.id');
        $user_id = User::where('u_id','=',$u_id)->value('id');

        $re = $redis->set('gold_room_total_looty:'.$user_id,$total_num);  //设置抽奖总数
        $res = $redis->set('gold_room_looty:'.$user_id,1); //设置当前抽奖次数为0
        $ree =  $redis->set('gold_room_gift_looty:'.$user_id,$id); //设置奖品
        if($re && $res && $ree)  $this->success('设置成功');
        else{
            $redis->rm('total_looty:'.$user_id);
            $redis->rm('looty:'.$user_id);
            $redis->rm('gift_looty:'.$user_id);
            $this->error('设置失败');
        }
    }

    //普通房间控制用户多少次获取全服
    public function sendUserGift()
    {
        $giftModel = new GoldRoomEggPrizeModel();
        $gift_list = $giftModel->getGiftData(1);
        return view()->assign('gift_list',$gift_list);
    }

    public function sendUserGiftData()
    {
        $redis = new Redis();
        $point = $redis->getKeys('looty:*');
        $arr = [];
        foreach($point as $k=>$v){
            $arr[] = intval(substr($v,6));
        }

        $data = User::where('id','IN',$arr)->field('id,u_id,nickname')->paginate();

        foreach($data as $k=>$v){
            $data[$k]['current_num'] = $redis->get('looty:'.$v['id']);  //目前抽奖的次数
            $data[$k]['total_num'] = $redis->get('total_looty:'.$v['id']);  //指定抽奖的次数
           // $data[$k]['id'] = $redis->get('gift_looty:'.$v['id']);  //指定抽中的奖品,库存id
            $id = $redis->get('gift_looty:'.$v['id']);  //指定抽中的奖品,库存id
            $gift_id = GoldRoomEggPrizeModel::where('id','=',$id)->value('gift_id');
            $gift_name = GiftUrl::where('id','=',$gift_id)->value('gift_name');
            $data[$k]['gift_name'] = $gift_name;

            if(empty($data[$k]['total_num']))  $data[$k]['total_num'] = 0;
            if(empty($data[$k]['id']))  $data[$k]['id'] = 0;
        }

        return json($data);

    }
    public function sendUserGiftAdd()
    {
        $redis = new Redis();
        $u_id = input('post.u_id');
        $total_num = input('post.total_num');
        $id = input('post.id');
//        $user_id = User::where('u_id','=',$u_id)->value('id');
       $user_id = User::where('u_number','=',$u_id)->value('user_id');

       $re = $redis->set('total_looty:'.$user_id,$total_num);  //设置抽奖总数
       $res = $redis->set('looty:'.$user_id,1); //设置当前抽奖次数为0
       $ree =  $redis->set('gift_looty:'.$user_id,$id); //设置奖品
       if($re && $res && $ree)  $this->success('设置成功');
       else{
           $redis->rm('total_looty:'.$user_id);
           $redis->rm('looty:'.$user_id);
           $redis->rm('gift_looty:'.$user_id);
           $this->error('设置失败');
       }
    }

    public function sendUserGiftDel(){

        $user_id = input('get.user_id');
        $redis = new Redis();
        $re = $redis->rm('looty:'.$user_id);
        $ree = $redis->rm('total_looty:'.$user_id);
        $res =  $redis->rm('gift_looty:'.$user_id);
        if($re && $ree && $res)
            return ['code'=>100,'msg'=>'取消成功'];
        else
            return ['code'=>-100,'msg'=>'取消失败'];
    }


    //控制第多少轮出全服
    public function setLoop()
    {
        $redis = new Redis();
        $custom_loop = json_decode($redis->get('custom_loop'),true);
        $data = [];
        //  halt($custom_loop);
        if(!empty($custom_loop)){

            foreach($custom_loop as $k => $v){

                $gift_id = GoldRoomEggPrizeModel::where('id','=',$k)->value('gift_id');
                $gift_name = GiftUrl::where('id','=',$gift_id)->value('gift_name');
                $arr['gift_name'] = $gift_name;
                $arr['loop_num'] = $v;
                $arr['prize_id'] = $k;
                $data[] = $arr;
            }
        }
        $giftModel = new GoldRoomEggPrizeModel();
        $gift_list = $giftModel->getGiftData(2);
        return view()->assign(['gift_list'=>$gift_list,'data'=>$data]);
    }

    public function setLoopAdd()
    {
        $redis = new Redis();
        $gift_id = input('post.gift_id');
        $loop_num = intval(input('post.loop_num'));

        $custom_loop = json_decode($redis->get('custom_loop'),true);

        if(empty($custom_loop)){
            $arr[$gift_id] = $loop_num;
            $re = $redis->set('custom_loop',json_encode($arr));
        }else{
            $custom_loop[$gift_id] = $loop_num;
            $re = $redis->set('custom_loop',json_encode($custom_loop));
        }
        // dump($custom_loop);

        if($re) return ['code'=>100,'msg'=>'设置成功'];
        else return ['code'=>-100,'msg'=>'设置失败'];
    }

    public function setLoopDel()
    {
        $prize_id = input('prize_id');
        $redis = new Redis();
        $custom_loop = json_decode($redis->get('custom_loop'),true);

        if(!empty($custom_loop[$prize_id])){

            unset($custom_loop[$prize_id]);
            $re = $redis->set('custom_loop',json_encode($custom_loop));
            if($re) return ['code'=>100,'msg'=>'修改成功'];
            else return ['code'=>-100,'msg'=>'修改失败'];


        }else return ['code'=>-100,'msg'=>'数据异常'];

    }


}